<template>
    <div id="poster" >
    <el-form class="login-container" label-position="left" :rules="loginRules" label-width="0px" :model="loginForm">
        <h3 class="login_title">系统登录</h3>
        <el-form-item prop="username">
            <el-input type="text" v-model="loginForm.username"  placeholder="账号"></el-input>
        </el-form-item>

        <el-form-item prop="password">
            <el-input type="password" v-model="loginForm.password"  placeholder="密码"></el-input>
        </el-form-item>

        <el-form-item style="width: 100%">
            <el-button type="primary" style="width: 100%;background: #505458;border: none" v-on:click="login">登录</el-button>
        </el-form-item>
        <el-form-item style="width: 100%">
           <span id="tips"> 没有账户?前往
            <router-link to="/register" style="text-decoration: none;" >注册</router-link> 界面</span>
        </el-form-item>

    </el-form>

    </div>
</template>


<script>
    import * as login from '../api/login'
    import Cookies from 'js-cookie'
    export default {
        name: "login",
        data() {
            return {
                loginForm: {
                    username: '',
                    password: ''
                },
                loginRules: {
                    username: [
                        { required: true, trigger: "blur", message: "请输入您的账号" }
                    ],
                    password: [
                        { required: true, trigger: "blur", message: "请输入您的密码" }
                    ]
                },
            }
        },
        methods: {
            login() {
                if(this.loginForm.username===""||this.loginForm.password===""){
                   this.$message({
                       message: '用户名或者密码不可为空',
                       type: 'error'
                   })
                }
                else {
                    const that = this
                login.login(this.loginForm.username,this.loginForm.password).then(function (response) {
                    if(response.status===200) {
                        let num = 0.5 //失效时间是几小时
                        let time = new Date(new Date().getTime() + num * 60 * 60 * 1000);
                        Cookies.set("authorization", response.headers.authorization, {
                            expires: time
                        })
                        that.$store.commit('updateUserName',that.loginForm.username)
                        that.$router.push("index")
                    }else if(response.data.code===400){
                        that.$message({
                            message: '用户名或者密码错误',
                            type: 'error'
                        })
                    }

                })
            }
            },
        }
    }
</script>

<style scoped >
    #poster {
        background:url("../assets/img/login-background.jpg") no-repeat;
        background-position: center;
        height: 100%;
        width: 100%;
        background-size: cover;
        position: fixed;
    }
    body{
        margin: 0px;
        padding: 0;
        overflow-y: hidden;
    }

    .login-container {
        border-radius: 15px;
        background-clip: padding-box;
        margin: 90px auto;
        width: 350px;
        padding: 35px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
        position: absolute;
        left: 30em;
        top: 7em;
    }
    #tips{
        position: relative;
        left: 2.7em;
        font-size: 17px;
    }
    .login_title {
        margin: 0px auto 40px auto;
        text-align: center;
        color: #505458;
    }
    ::-webkit-scrollbar {
        width: 0px;
        height: 0px;
    }
    ::-webkit-scrollbar-track-piece {
        background-color: rgba(0, 0, 0, 0.2);
        -webkit-border-radius: 6px;
    }

    ::-webkit-scrollbar-thumb:vertical {
        /*height: 2px;*/
        background-color: rgba(223, 239, 238, 0.7);
        -webkit-border-radius: 6px;
    }

    ::-webkit-scrollbar-thumb:horizontal {
        width: 0px;
        background-color: rgba(223, 239, 238, 0.7);
        -webkit-border-radius: 2px;
    }


</style>
